package com.qfedu.fmmll.submit.service.impl;

import com.qfedu.fmmall.entity.Orders;
import com.qfedu.fmmall.entity.ProductSku;
import com.qfedu.fmmall.vo.ShoppingCartVO;
import com.qfedu.fmmll.submit.service.OrderSubmitService;
import com.qfedu.fmmll.submit.service.feign.OrderAddClient;
import com.qfedu.fmmll.submit.service.feign.OrderItemAddClient;
import com.qfedu.fmmll.submit.service.feign.ShupcartDelClient;
import com.qfedu.fmmll.submit.service.feign.StockUpdateClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author : yanBin
 * @date :2022/2/10 15:49
 */
@Service
public class OrderSubmitServiceImpl implements OrderSubmitService {
    private static final Logger log = LoggerFactory.getLogger(OrderSubmitServiceImpl.class);

    @Autowired
    private OrderAddClient orderAddClient;

    @Autowired
    private OrderItemAddClient orderItemAddClient;

    @Autowired
    private StockUpdateClient stockUpdateClient;

    @Autowired
    private ShupcartDelClient shupcartDelClient;

    @Override
    public Map<String, String> addOrders(String cids, Orders orders) {
        Map<String, String> map = new HashMap<>();
        try {
            //1.保存订单（校验库存）order-add
            String orderId = UUID.randomUUID().toString().replace("-", "");
            orders.setOrderId(orderId);
            List<ShoppingCartVO> shoppingCartVOList = orderAddClient.saveOrder(orders, cids);
            if (CollectionUtils.isEmpty(shoppingCartVOList)) {
                return null;
            }

            //2.保存订单快照 order-item-add
            int i = orderItemAddClient.addItem(shoppingCartVOList, orderId);
            if (i <= 0) {
                return null;
            }
            System.out.println(i);
            //3.修改库存 stock-update
            StringBuffer untitled = new StringBuffer();
            List<ProductSku> productSkuList = new ArrayList<>();
            for (ShoppingCartVO sc : shoppingCartVOList) {
                ProductSku productSku = new ProductSku();
                if (sc.getSkuStock() - Integer.valueOf(sc.getCartNum()) < 0) {
                    return null;
                }
                productSku.setStock(sc.getSkuStock() - Integer.valueOf(sc.getCartNum()));
                productSku.setSkuId(sc.getSkuId());
                productSkuList.add(productSku);
                untitled.append(sc.getProductName());
                untitled.append(",");
            }
            int update = stockUpdateClient.update(productSkuList);
            if (update <= 0) {
                return null;
            }
            //4.删除购物车 shupcart-del
            int delete = shupcartDelClient.delete(cids);
            if (delete <= 0) {
                return null;
            }
            map.put("orderId", orderId);
            map.put("productNames", untitled.toString());
            System.out.println(map);
            return map;
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            return null;
        }

    }
}
